﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDImageView.h"
#include "actions/CCActionInterval.h"
#include "actions/CCActionInstant.h"

static int viewCount = 0;


GDImageView::GDImageView(void)
:image(NULL)
, tipLabel(NULL)
, m_pDelegate(NULL)
, m_eImageViewScaleType(CAImageViewScaleTypeFitImageCrop)
, isAddImage(false)
, isImageViewRequestFinished(false)
, isSetUrl(false)
,i_url("")
{
	++viewCount;
	//CCLog("GDImageView = %d", viewCount);
}


GDImageView::~GDImageView(void)
{
	CC_SAFE_RELEASE_NULL(image);
	--viewCount;
	//CCLog("~GDImageView = %d",viewCount);
}

bool GDImageView::init(){
	if (!CAView::init()){
		return false;
	}

	return true;
}

void GDImageView::initView(CAImage *defaultImage, const HttpGetImageType& type){
	this->setColor(CAColor_clear);
	image = CommonUrlImageView::createWithFrame(this->getBounds());
	image->retain();
	image->setVisible(false);
	image->setDelegate(this);
	image->setImageType(type);
	if (defaultImage){
		setDefaultImage(defaultImage);
	}
	else{
		
		//image->setImage(CAImage::CC_WHITE_IMAGE());
	}
	//
}

GDImageView* GDImageView::createWithFrame(const CCRect& rect, CAImage *defaultImage, const HttpGetImageType& type)
{
	GDImageView * pRet = new GDImageView();
	if (pRet && pRet->initWithFrame(rect))
	{
		pRet->initView(defaultImage, type);
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

GDImageView* GDImageView::createWithFrame(const CCRect& rect, const CAColor4B& color4B, CAImage *defaultImage, const HttpGetImageType& type)
{
	GDImageView * pRet = new GDImageView();
	if (pRet && pRet->initWithFrame(rect, color4B))
	{
		pRet->initView(defaultImage, type);
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

GDImageView* GDImageView::createWithCenter(const CCRect& rect, CAImage *defaultImage, const HttpGetImageType& type)
{
	GDImageView * pRet = new GDImageView();
	if (pRet && pRet->initWithCenter(rect))
	{
		pRet->initView(defaultImage, type);
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

GDImageView* GDImageView::createWithCenter(const CCRect& rect, const CAColor4B& color4B, CAImage *defaultImage, const HttpGetImageType& type)
{
	GDImageView * pRet = new GDImageView();
	if (pRet && pRet->initWithCenter(rect, color4B))
	{
		pRet->initView(defaultImage, type);
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

void GDImageView::setColor(const CAColor4B& color){
	CAView::setColor(color);
}

void GDImageView::setFrame(const CCRect &rect){
	//CCLog("%s ---------------", __FUNCTION__);
	CAView::setFrame(rect);
	if (image != NULL && isAddImage){
		//CCLog("%s ===============", __FUNCTION__);
		image->setFrame(CCRect(0, 0, rect.size.width, rect.size.height));
		resetImageRect();
	}
}

void GDImageView::setDefaultImage(CAImage *image, bool isResetImageRect){
	this->image->setImage(image);
	this->image->setVisible(true);
	if (!isAddImage){
		this->addSubview(this->image);
		isAddImage = true;
	}
	if (isResetImageRect){
		resetImageRect();
	}
}

void GDImageView::setImageUrl(const std::string& url){
	image->setUrl(url); // 动态加载图片
}

void GDImageView::setUrl(const std::string& url){
	if (i_url == url || image==NULL) {
        return;
    }
    i_url = url;
	image->setVisible(false);
	image->setUrl(url); // 动态加载图片
	isSetUrl = true;
}

std::string GDImageView::getUrl()
{
	return i_url;
}

void GDImageView::setTipLabel(CALabel *tipLabel){
	this->tipLabel = tipLabel;
}

CommonUrlImageView* GDImageView::getCommonUrlImageView(){
	return image;
}

bool  GDImageView::isImageRequestFinished(){
	return this->isImageViewRequestFinished;
}

bool GDImageView::getIsSetUrl(){
	return isSetUrl;
}

void GDImageView::setSuperview(CAView* superview)
{
    CAView::setSuperview(superview);
    if (superview == NULL && image)
    {
        image->setDelegate(NULL);
    }
}

/* 网络图片下载完成回调方法 */
void GDImageView::imageViewRequestFinished(CommonUrlImageView* imageView){
	if (!isAddImage && image){
		this->addSubview(image);
		isAddImage = true;
	}
    
    if(image == NULL){
        return;
    }
    
    image->setAlpha(0.0);
	isImageViewRequestFinished = true;
	image->setVisible(true);
    //this->image = imageView;
    resetImageRect();
    
    image->runAction(CCSequence::createWithTwoActions(CCDelayTime::create(-0.2), CCFadeIn::create(1)));

	isLoad = true;

	#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		
	#endif

	if (m_pDelegate){ // 事件抛出
		m_pDelegate->imageViewRequestFinished(imageView);
	}
}

void GDImageView::resetImageRect(){
	if (m_eImageViewScaleType == CAImageViewScaleTypeFitImageXY){
		resetImageRectXY();
	}else if (m_eImageViewScaleType == CAImageViewScaleTypeFitImageCrop){
		resetImageRectCrop();
	} else if (m_eImageViewScaleType == CAImageViewScaleTypeFitImageInside){
		resetImageRectInside();
	}
}

void GDImageView::resetImageRectXY(){
	if (this->image == NULL || this->image->getImage() == NULL){
		return;
	}
	std::string tips = "";
	CCSize thisSize = this->getBounds().size; // 视图大小
	
	CCSize imageSize = this->image->getImage()->getContentSize(); // 图片像素大小

	if (thisSize.width == imageSize.width && thisSize.height == imageSize.height){ // 如果长宽都相等
		image->setFrame(this->getBounds());
		image->setImageRect(CCRect(0, 0, imageSize.width, imageSize.height));
		tips = CCString::createWithFormat("图片(%f,%f)高度宽度刚好合适", imageSize.width, imageSize.height)->m_sString;
	}
	else if (imageSize.width < thisSize.width){ // 图片宽度小于显示宽度
		if (imageSize.height <= thisSize.height){ // 如果图片高度小于显示高度
			image->setCenter(CCRect(thisSize.width / 2, thisSize.height / 2, imageSize.width, imageSize.height)); // 图片居中显示
			image->setImageRect(CCRect(0, 0, imageSize.width, imageSize.height));
			tips = CCString::createWithFormat("图片(%f,%f)高度宽度均不够，居中显示", imageSize.width, imageSize.height)->m_sString;
		}
		else{ // 高度超出
			image->setCenter(CCRect(thisSize.width / 2, thisSize.height / 2, imageSize.width, thisSize.height)); // 图片居中显示
			image->setImageRect(CCRect(0, (imageSize.height - thisSize.height) / 2, imageSize.width, thisSize.height)); // 截取图片中间部位
			tips = CCString::createWithFormat("图片(%f,%f)宽度不够，高度超出", imageSize.width, imageSize.height)->m_sString;
		}
	}
	else{ // 宽度大于屏幕宽度
		if (imageSize.height < thisSize.height){ // 如果图片高度小于显示高度
			image->setCenter(CCRect(thisSize.width / 2, thisSize.height / 2, thisSize.width, imageSize.height)); // 图片居中显示
			image->setImageRect(CCRect((imageSize.width - thisSize.width) / 2, 0, imageSize.width, thisSize.height)); // 截取图片中间部位
			tips = CCString::createWithFormat("图片(%f,%f)高度不够，宽度太宽", imageSize.width, imageSize.height)->m_sString;
		}
		else{ // 长和宽都超出
			float thisSizeWH = thisSize.width / thisSize.height; //视图的宽高比
			float imageSizeWH = imageSize.width / imageSize.height; //图片的宽高比

			image->setFrame(this->getBounds()); // 图片铺满视图


			if (imageSizeWH>thisSizeWH){ // 如果宽度比例大，按高度缩放
				float scaleWidth = thisSize.width * imageSize.height / thisSize.height;
				image->setImageRect(CCRect((imageSize.width - scaleWidth) / 2, 0, scaleWidth, imageSize.height));
				tips = CCString::createWithFormat("图片(%f,%f)太宽，显示width:%f,height:%f,显示point:(%f,%f)", imageSize.width, imageSize.height, scaleWidth, imageSize.height, (imageSize.width - scaleWidth) / 2, 0.0f)->m_sString;
			}
			else if (imageSizeWH < thisSizeWH){ // 如果高度比例大，按宽度缩放
				float scaleHeight = thisSize.height * imageSize.width / thisSize.width;
				image->setImageRect(CCRect(0, (imageSize.height - scaleHeight) / 2, imageSize.width, scaleHeight));
				tips = CCString::createWithFormat("图片(%f,%f)太高，显示width:%f,height:%f,显示point:(%f,%f)", imageSize.width, imageSize.height, imageSize.width, scaleHeight, 0.0f, (imageSize.height - scaleHeight) / 2)->m_sString;
			}
			else{
				image->setImageRect(CCRect(0, 0, imageSize.width, imageSize.height));
				tips = CCString::createWithFormat("图片(%f,%f)高宽比例刚刚好，直接缩放显示", imageSize.width, imageSize.height)->m_sString;
			}
		}
	}

	if (tipLabel){
		tipLabel->setText(tips);
	}
	//CCLog("%s tips:%s", __FUNCTION__, tips.c_str());
}

void GDImageView::resetImageRectCrop(){
    
	if (this->image == NULL || this->image->getImage() == NULL){
		return;
	}
	std::string tips = "";
	CCSize thisSize = this->getBounds().size; // 视图大小
	CCSize imageSize = this->image->getImage()->getContentSize(); // 图片像素大小

	float thisSizeWH = thisSize.width / thisSize.height; //视图的宽高比
	float imageSizeWH = imageSize.width / imageSize.height; //图片的宽高比

	image->setFrame(this->getBounds()); // 图片铺满视图


	if (imageSizeWH>thisSizeWH){ // 如果宽度比例大，按高度缩放
		float scaleWidth = thisSize.width * imageSize.height / thisSize.height;
		image->setImageRect(CCRect((imageSize.width - scaleWidth) / 2, 0, scaleWidth, imageSize.height));
		tips = CCString::createWithFormat("图片(%f,%f)太宽，显示width:%f,height:%f,显示point:(%f,%f)", imageSize.width, imageSize.height, scaleWidth, imageSize.height, (imageSize.width - scaleWidth) / 2, 0.0f)->m_sString;
	}
	else if (imageSizeWH < thisSizeWH){ // 如果高度比例大，按宽度缩放
		float scaleHeight = thisSize.height * imageSize.width / thisSize.width;
		image->setImageRect(CCRect(0, (imageSize.height - scaleHeight) / 2, imageSize.width, scaleHeight));
		tips = CCString::createWithFormat("图片(%f,%f)太高，显示width:%f,height:%f,显示point:(%f,%f)", imageSize.width, imageSize.height, imageSize.width, scaleHeight, 0.0f, (imageSize.height - scaleHeight) / 2)->m_sString;
	}
	else{
		image->setImageRect(CCRect(0, 0, imageSize.width, imageSize.height));
		tips = CCString::createWithFormat("图片(%f,%f)高宽比例刚刚好，直接缩放显示", imageSize.width, imageSize.height)->m_sString;
	}
		

	if (tipLabel){
		tipLabel->setText(tips);
	}
	//CCLog("%s tips:%s", __FUNCTION__, tips.c_str());
}


void GDImageView::resetImageRectInside(){
	if (this->image == NULL || this->image->getImage() == NULL){
		return;
	}
	std::string tips = "";
	CCSize thisSize = this->getBounds().size; // 视图大小
	CCSize imageSize = this->image->getImage()->getContentSize(); // 图片像素大小

	float thisSizeWH = thisSize.width / thisSize.height; //视图的宽高比
	float imageSizeWH = imageSize.width / imageSize.height; //图片的宽高比

	if (imageSizeWH>thisSizeWH){ // 如果宽度比例大，按宽度缩放
		float scaleHeight = imageSize.height * thisSize.width / imageSize.width;
		image->setFrame(CCRect(0, (thisSize.height - scaleHeight) / 2, thisSize.width, scaleHeight));
		tips = CCString::createWithFormat("图片(%f,%f)太高，显示width:%f,height:%f,显示point:(%f,%f)", imageSize.width, imageSize.height, imageSize.width, scaleHeight, 0.0f, (imageSize.height - scaleHeight) / 2)->m_sString;
	}
	else if (imageSizeWH < thisSizeWH){ // 如果高度比例大，按高度缩放
		float scaleWidth = imageSize.width * thisSize.height / imageSize.height;
		image->setFrame(CCRect((thisSize.width - scaleWidth) / 2,0, scaleWidth, thisSize.height));
		tips = CCString::createWithFormat("图片(%f,%f)太宽，显示width:%f,height:%f,显示point:(%f,%f)", imageSize.width, imageSize.height, scaleWidth, imageSize.height, (imageSize.width - scaleWidth) / 2, 0.0f)->m_sString;
	}
	else{
		image->setFrame(this->getBounds()); // 图片铺满视图
		tips = CCString::createWithFormat("图片(%f,%f)高宽比例刚刚好，直接缩放显示", imageSize.width, imageSize.height)->m_sString;
	}

	if (tipLabel){
		tipLabel->setText(tips);
	}
	//CCLog("%s tips:%s", __FUNCTION__, tips.c_str());
}